﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SiteFox.WebParts.Navigation
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    
    public class QuickLinksWP : WebPart
    {
        #region PublicProperies
        private string _CssClass = "";
        [Personalizable(true)]
        public new string CssClass
        {
            get { return _CssClass; }
            set { _CssClass = value; }
        }

        private int _QuickLinksId = -1;
        [Personalizable(true)]
        public int QuickLinksId
        {
            get { return _QuickLinksId; }
            set { _QuickLinksId = value; }
        }
        #endregion
        
        public override EditorPartCollection CreateEditorParts()
        {
            ArrayList _editorArray = new ArrayList();
            QuickLinks_Editor _edPart = new QuickLinks_Editor();
            _edPart.ID = this.ID + "_QuickLinks";
            _editorArray.Add(_edPart);
            EditorPartCollection _editorParts = new EditorPartCollection(_editorArray);
            return _editorParts;
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            var _QuickLinks = SiteFox.Business.Manager.QuickLink.GetByQuickLinks(_QuickLinksId);
            Literal _lit_ul_Start = new Literal();
            _lit_ul_Start.Text = "<ul class='" + _CssClass + "'>";
            Controls.Add(_lit_ul_Start);
            for (int i = 0; i < _QuickLinks.Count; i++) 
            {
                var _QuickLink = _QuickLinks[i];
                Literal _li_Start = new Literal();
                _li_Start.Text = "<li>";
                Controls.Add(_li_Start);
                HyperLink _hl_QuickLink = new HyperLink();
                _hl_QuickLink.Text = _QuickLink.Title;
                _hl_QuickLink.Target = _QuickLink.Target;
                _hl_QuickLink.ToolTip = _QuickLink.Description;
                _hl_QuickLink.NavigateUrl = _QuickLink.Url;
                Controls.Add(_hl_QuickLink);
                Literal _li_End = new Literal();
                _li_End.Text = "</li>";
                Controls.Add(_li_End);
            }
            Literal _lit_ul_End = new Literal();
            _lit_ul_End.Text = "</ul>";
            Controls.Add(_lit_ul_End);
        }

        // Create a custom EditorPart to edit the WebPart control.
        [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
        private class QuickLinks_Editor : EditorPart
        {
            Label _lab_CssClass;
            TextBox _tb_CssClass;
            Label _lab_QuickLinksList;
            DropDownList _ddl_QuickLinksList;
            Literal _lit_Brake;
            
            public override bool ApplyChanges()
            {
                QuickLinksWP _part = (QuickLinksWP)WebPartToEdit;
                _part._QuickLinksId = Convert.ToInt32(_ddl_QuickLinksList.SelectedValue);
                _part._CssClass = _tb_CssClass.Text;
                return true;
            }
            public override void SyncChanges()
            {
                EnsureChildControls();
                QuickLinksWP _part = (QuickLinksWP)WebPartToEdit;
                if (_part._QuickLinksId > -1) 
                { 
                    var _QuickLinks = SiteFox.Business.Manager.QuickLinks.GetByID(_part._QuickLinksId);
                    if (_QuickLinks != null)
                    {
                        _ddl_QuickLinksList.SelectedValue = _part._QuickLinksId.ToString();
                    }
                }
                _tb_CssClass.Text = _part._CssClass;
            }
            protected override void CreateChildControls()
            {
                Controls.Clear();
                _lit_Brake = new Literal();
                _lit_Brake.Text = "<br />";
                _lab_CssClass = new Label();
                _lab_CssClass.Text = "CssClass";
                _lab_CssClass.Width = Unit.Percentage(95);
                Controls.Add(_lab_CssClass);
                Controls.Add(_lit_Brake);
                _tb_CssClass = new TextBox();
                _tb_CssClass.Width = Unit.Percentage(95);
                Controls.Add(_tb_CssClass);
                Controls.Add(_lit_Brake);
                _lab_QuickLinksList = new Label();
                _lab_QuickLinksList.Text = "QuickLinks";
                _lab_QuickLinksList.Width = Unit.Percentage(95);
                Controls.Add(_lab_QuickLinksList);
                Controls.Add(_lit_Brake);
                _ddl_QuickLinksList = new DropDownList();
                _ddl_QuickLinksList.Width = Unit.Percentage(95);
                _ddl_QuickLinksList.DataSource = SiteFox.Business.Manager.QuickLinks.GetAll();
                _ddl_QuickLinksList.DataTextField = "Name";
                _ddl_QuickLinksList.DataValueField = "ID";
                _ddl_QuickLinksList.DataBind();
                Controls.Add(_ddl_QuickLinksList);
            }
        }
    }
}

